from widgets.scriptWidgets.ScriptWidget import ScriptWidget
from enums.WidgetType import WidgetType
from PySide6.QtWidgets import QLabel, QPushButton, QComboBox
from ..ScriptInput import ScriptInput
from ..ScriptTimeInput import ScriptTimeInput
from enums.ZhButtonType import ZhButtonType
from enums.ButtonType import ButtonType
import time
from util.Mouse import MouseUtil


class Drag(ScriptWidget):
    def __init__(self, initType=None):
        super().__init__(WidgetType.DRAG)

        self.pressTime = 0
        self.initType = initType

        self.labTitle = QLabel('拖拽:', self)
        self.labTitle.setFixedSize(30, self.lineHeight)
        self.addWidget(self.labTitle)

        self.newLineSpaceWidth = self.labTitle.width() + self.intervalX

        # from
        labFromTitle = QLabel('从:', self)
        labFromTitle.setFixedSize(15, self.lineHeight)
        self.addWidget(labFromTitle)

        self.inputFromX = ScriptInput(self, 'x')
        self.inputFromX.text = 0
        self.addWidget(self.inputFromX)

        self.inputFromY = ScriptInput(self, 'y')
        self.inputFromY.text = 0
        self.addWidget(self.inputFromY)

        if initType == 'edit' or initType == 'create':
            self.addOperate()

        # to
        self.newLine()
        labToTitle = QLabel('到:', self)
        labToTitle.setFixedSize(15, self.lineHeight)
        self.addWidget(labToTitle)

        self.inputToX = ScriptInput(self, 'x')
        self.inputToX.text = 0
        self.addWidget(self.inputToX)

        self.inputToY = ScriptInput(self, 'y')
        self.inputToY.text = 0
        self.addWidget(self.inputToY)

        # time
        self.newLine()
        self.inputTime = ScriptTimeInput(self, '时间')
        self.addWidget(self.inputTime)

        # buttonType
        self.comButtonType = QComboBox(self)
        self.comButtonType.addItems(ZhButtonType.values())
        self.comButtonType.setCurrentText(ZhButtonType.values()[0])
        self.addWidget(self.comButtonType)

        self.newLine()
        self.btnTrack = QPushButton(self)
        self.btnTrack.setText('模拟')
        self.btnTrack.clicked.connect(self.startClickListener)
        self.addWidget(self.btnTrack)

    def onMouseClick(self, fromX, fromY, toX, toY, dragTime, button):
        self.inputFromX.text = fromX
        self.inputFromY.text = fromY
        self.inputToX.text = toX
        self.inputToY.text = toY
        self.inputTime.text = int(dragTime)
        buttonName = button.name.upper()
        self.comButtonType.setCurrentText(ZhButtonType[buttonName].value)

    def startClickListener(self):
        MouseUtil.listenDrag(self.onMouseClick)

    @property
    def data(self):
        zhType = ZhButtonType(self.comButtonType.currentText())
        buttonType = ButtonType[zhType.name].value
        return {
            "type": self.type.value,
            'from_x': int(self.inputFromX.text),
            'from_y': int(self.inputFromY.text),
            'to_x': int(self.inputToX.text),
            'to_y': int(self.inputToY.text),
            'time': int(self.inputTime.text),
            'button': buttonType,
            'enable': self.switchEnable.isChecked()
        }

    @data.setter
    def data(self, data):
        self.setData(data)
        self.inputFromX.text = data['from_x']
        self.inputFromY.text = data['from_y']
        self.inputToX.text = data['to_x']
        self.inputToY.text = data['to_y']
        self.inputTime.text = data['time']
        self.comButtonType.setCurrentText(ZhButtonType[data['button'].upper()].value)
